package com.smec.remes.module.register.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutMaintainBinding;
import com.smec.remes.base.SmecBaseFragment;
import com.smec.remes.bean.SearchContract;
import com.smec.remes.module.register.SmecRegisterPresenter;
import com.smec.remes.module.register.adapter.SmecContractAdapter;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.WmsConstant;

import java.util.ArrayList;

import static com.smec.remes.module.register.fragment.SmecContractStationFragment.CONTRACT;

/**
 * Created by xupeizuo on 2018/3/7.
 */

public class SmecMaintain extends SmecBaseFragment<SmecRegisterPresenter> {

    private LayoutMaintainBinding layoutMaintainBinding;

    private String  status;
    private int pageNumber=1;
    private String condition="";
    private SmecContractAdapter smecContractAdapter;
    private Handler mHandler=new Handler();
    @Override
    protected SmecRegisterPresenter getPersenter() {
        return new SmecRegisterPresenter(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        layoutMaintainBinding=DataBindingUtil.inflate(inflater, R.layout.layout_maintain,container,false);
        smecContractAdapter=new SmecContractAdapter(getActivity());
        layoutMaintainBinding.contractRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        layoutMaintainBinding.contractRecyclerView.setAdapter(smecContractAdapter);
        initData();
        _search();
        return layoutMaintainBinding.getRoot();
    }

    public void initData(){
        layoutMaintainBinding.ptr.setHeaderView(WmsConstant.getSinaRefreshView());
        layoutMaintainBinding.ptr.setBottomView(new LoadingView(getActivity()));
        layoutMaintainBinding.ptr.setAutoLoadMore(true);
        layoutMaintainBinding.ptr.setEnableOverScroll(false);
        layoutMaintainBinding.ptr.setEnableLoadmore(true);
        layoutMaintainBinding.ptr.setEnableRefresh(true);
        layoutMaintainBinding.ptr.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                pageNumber=1;
                status="onRefresh";
                mPresenter._Month(condition,"MONTH",pageNumber);
                pageNumber+=1;
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                status="onLoadMore";
                mPresenter._Month(condition,"MONTH",pageNumber);
                pageNumber+=1;
            }

        });
        layoutMaintainBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                layoutMaintainBinding.ptr.startRefresh();
            }
        },500);
    }

    public void _search(){
        layoutMaintainBinding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchByKeyWord();
            }
        });
        layoutMaintainBinding.edSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    searchByKeyWord();
                }
                return false;
            }
        });
        layoutMaintainBinding.edSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                layoutMaintainBinding.ivDelete.setVisibility(View.VISIBLE);
                layoutMaintainBinding.ivSearch.setImageResource(R.drawable.smec_remes_lightsearch);
            }
        });
        layoutMaintainBinding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                condition="";
                layoutMaintainBinding.edSearch.setText("");
            }
        });
    }

    private void searchByKeyWord(){
        layoutMaintainBinding.ptr.setEnableRefresh(true);
        CommonUtils.hideImmManager(layoutMaintainBinding.getRoot());
        condition=layoutMaintainBinding.edSearch.getText().toString();
        layoutMaintainBinding.ptr.startRefresh();
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag("MAINTAIN")
            }
    )

    public void _getContract( ArrayList<SearchContract> arrayList){
        if(CommonUtils.notEmpty(arrayList)){
            smecContractAdapter.getList(arrayList,status);
        }else {
            if(status.equals("onRefresh")){
                smecContractAdapter.clearList();
            }
        }

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(status.equals("onRefresh")){
                    layoutMaintainBinding.ptr.finishRefreshing();
                }else {
                    layoutMaintainBinding.ptr.finishLoadmore();
                }

            }
        },500);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag("MAINTAINFAIL")
            }
    )

    public void _contractFail(String error){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(status.equals("onRefresh")){
                    layoutMaintainBinding.ptr.finishRefreshing();
                }else {
                    layoutMaintainBinding.ptr.finishLoadmore();
                }
            }
        },500);
    }

    public static SmecMaintain newInstance() {
        Bundle args = new Bundle();
        SmecMaintain fragment = new SmecMaintain();
        fragment.setArguments(args);
        return fragment;
    }
}
